package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.Shop;

/**
 * Created by AAA on 2017/4/17.
 */

public class UserTaskAdapter extends BaseAdapter{

    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private Context context;
    private List<Schedule> list;

    public UserTaskAdapter(Context context,List<Schedule> list){
        this.context=context;
        this.list=list;
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.schedule_task_item,parent,false);
            holder=new ViewHolder();
            holder.TaskTitle= (TextView) convertView.findViewById(R.id.TaskTitle);
            holder.startTime= (TextView) convertView.findViewById(R.id.startTime);
            holder.shopName= (TextView) convertView.findViewById(R.id.shopName);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        Schedule bean =list.get(position);
        if(list==null){

        }else{
            if(bean.getSchEndTime()!=null)
                holder.startTime.setText(bean.getSchStartTime().replace('T',' '));
            else
                holder.startTime.setText(bean.getSchStartTime().split("T")[0]);
            int shopID=bean.getShopID();
            try {
                if(shopID==0){
                    holder.shopName.setText("非门店任务");
                    holder.shopName.setVisibility(View.GONE);
                }
                else if(shopID==-1&&bean.getIsAssign()==-1)
                {
                    if(bean.getSubtitle()=="备注")
                    {
                        holder.shopName.setText("备注");
                    }
                }
                else{
                    Shop shop=db.selector(Shop.class).where("ID","=",shopID).findFirst();
                    if(shop==null){
                        holder.shopName.setText("正在取名中..");
                    }else{
                        String shopname=shop.getShopName();
                        holder.shopName.setText(shopname);
                    }
                }
            } catch (DbException e) {
                e.printStackTrace();
            }
        }
        holder.TaskTitle.setText(bean.getTitle());


        return convertView;
    }

    class ViewHolder{
        TextView TaskTitle;
        TextView startTime;
        TextView shopName;
    }
}
